package com.example.football;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.football.dao.PeopleDao;
import com.example.football.entity.People;
import com.android.volley.Request;
import com.android.volley.Response;
import com.example.football.util.JDBCUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class RegisterActivity extends AppCompatActivity {

    private EditText edt_username;
    private EditText edt_password;
    private EditText edt_address;
    private EditText editText;
//    private static final String TAG = "mysql-buwan-register";
//    EditText account = null;
//    EditText password = null;
//    EditText name = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        ActionBar actionBar=getSupportActionBar();
        if(actionBar!=null){
            actionBar.hide();
        }
//        account = findViewById(R.id.account);
//        password = findViewById(R.id.password);
//        name = findViewById(R.id.name);
    }


    public void log(View view){
        startActivity(new Intent(getApplicationContext(),MainActivity.class));
    }

    public void register(View view) {

        edt_username=findViewById(R.id.account);
        edt_password=findViewById(R.id.password);
        edt_address=findViewById(R.id.name);
        editText = findViewById(R.id.byteam);

        String account = edt_username.getText().toString().trim();
        String password = edt_password.getText().toString().trim();
        String name = edt_address.getText().toString().trim();
        String byteam = editText.getText().toString().trim();
        if(account.equals("")||password.equals("")||name.equals("")||byteam.equals("")){
            Toast.makeText(RegisterActivity.this, "请填写完整", Toast.LENGTH_SHORT).show();
        }else {
            JSONObject jsonObject=new JSONObject();
            try {
                jsonObject.put("account",account);
                jsonObject.put("password",password);
                jsonObject.put("name",name);
                jsonObject.put("byteam",byteam);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            String url="http://"+ JDBCUtils.ip +":9000/APPregister";
            RequestQueue requestQueue= Volley.newRequestQueue(RegisterActivity.this);
            JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.POST, url,jsonObject, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject jsonObject) {
                    try {
                        Log.d("注册信息", jsonObject.toString());
                        String msg = jsonObject.getString("msg");
                        Toast.makeText(RegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
                        if(msg.equals("注册成功")){
                            JSONObject detail = jsonObject.getJSONObject("detail");
                            final String username_login = detail.getString("account");
                            final String byteam1 = detail.getString("byteam");
                            final Integer id = detail.getInt("id");
                            Intent intent=new Intent(RegisterActivity.this,RegisterliketeamActivity.class);
                            intent.putExtra("id",id);
                            intent.putExtra("account",username_login);
                            intent.putExtra("byteam",byteam1);
                            startActivity(intent);

                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(RegisterActivity.this, "网络出错", Toast.LENGTH_SHORT).show();
                }
            });
            requestQueue.add(jsonObjectRequest);
        }
    }

//    public void register(View view){
//
//        String userAccount1 = account.getText().toString();
//        String userPassword1 = password.getText().toString();
//        String userName1 = name.getText().toString();
//
//
//        final People user = new People();
//
//        user.setAccount(userAccount1);
//        user.setPassword(userPassword1);
//        user.setName(userName1);
//
//        new Thread(){
//            @Override
//            public void run() {
//
//                int msg = 0;
//
//                PeopleDao userDao = new PeopleDao();
//
//                People uu = userDao.findUser(user.getAccount());
//                if(uu != null){
//                    msg = 1;
//                }
//                else{
//                    boolean flag = userDao.register(user);
//                    if(flag){
//                        msg = 2;
//                    }
//                }
//                hand.sendEmptyMessage(msg);
//
//            }
//        }.start();
//
//
//    }
//    @SuppressLint("HandlerLeak")
//    final Handler hand = new Handler()
//    {
//        public void handleMessage(Message msg) {
//            if(msg.what == 0) {
//                Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();
//            } else if(msg.what == 1) {
//                Toast.makeText(getApplicationContext(),"该账号已经存在，请换一个账号",Toast.LENGTH_LONG).show();
//            } else if(msg.what == 2) {
//                Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show();
//                Intent intent = new Intent();
//                //将想要传递的数据用putExtra封装在intent中
//                intent.putExtra("a","注册");
//                setResult(RESULT_CANCELED,intent);
//                finish();
//            }
//        }
//    };

}
